package com.ai.cltcore.service.common.impl;

import java.util.Map;

import com.ai.cltcore.service.common.ICacheService;
import com.ai.cltcore.util.DedicException;
import com.ai.cltcore.util.JedisUtil;

import redis.clients.jedis.Protocol;

public class CacheServiceImpl implements ICacheService {
	// 调用成功标识
	private static final String IS_OK = Protocol.Keyword.OK.name();
	
	public boolean put2Cache(String cacheKey, String value)  throws DedicException {
		String result = JedisUtil.getCluster().set(cacheKey, value);
		return IS_OK.equals(result);
	}
	
	public boolean put2Cache(String cacheKey, String value, int seconds)  throws DedicException {
		String result = JedisUtil.getCluster().setex(cacheKey, seconds, value);
		return IS_OK.equals(result);
	}
	
	public String getFromCache(String cacheKey) throws DedicException  {
		return JedisUtil.getCluster().get(cacheKey);
	}
	
	public long incr(String key) throws DedicException {
		return JedisUtil.getCluster().incr(key);
	}
	
	public long decr(String key) throws DedicException {
		return JedisUtil.getCluster().decr(key);
	}

	public String putMap(String key, Map<String, String> map) throws DedicException {
		return JedisUtil.getCluster().hmset(key, map);
	}

	public Map<String, String> getMap(String key) throws DedicException {
		return JedisUtil.getCluster().hgetAll(key);
	}
	
	public Long lpush(String key, String... values) throws DedicException {
		return JedisUtil.getCluster().lpush(key, values);
	}
	
	public Long rpush(String key, String... values) throws DedicException {
		return JedisUtil.getCluster().rpush(key, values);
	}
	
	public String lpop(String key) throws DedicException {
		return JedisUtil.getCluster().lpop(key);
	}
	
	public String rpop(String key) throws DedicException {
		return JedisUtil.getCluster().rpop(key);
	}

	@Override
	public void delFromCache(String cacheKey) throws DedicException {
		JedisUtil.getCluster().del(cacheKey);
		
	}
	@Override
	public String hget(String key, String field) throws DedicException {
			return JedisUtil.getCluster().hget(key, field);
	}
	@Override
	public void hdel(String key, String field) throws DedicException {
		JedisUtil.getCluster().hdel(key, field);
	}
	@Override
	public void hset(String key, String field, String value)
			throws DedicException {
		JedisUtil.getCluster().hset(key, field, value);
	}
	@Override
	public void expire(String key, int seconds)
			throws DedicException {
		JedisUtil.getCluster().expire(key, seconds);
	}
}